package com.design.prototype;

import java.io.*;

/**
 * @author leali
 * @title: WeeklyLog
 * @projectName design
 * @description: 工作周报WeeklyLog：具体原型类，考虑到代码的可读性和易理解性，只列出部分与模式相关的核心代码
 * @date 2022/1/2 20:27
 */
public class WeeklyLog implements Cloneable, Serializable {
    private String name;
    private String date;
    private String content;

    private Attachment attachment;

    /**
     * 克隆方法clone()，此处使用Java语言提供的克隆机制
     * 浅拷贝
     * 深拷贝需要使用 Serializable
     *
     * @return WeeklyLog
     */
    @Override
    public WeeklyLog clone() {
        Object obj = null;
        try {
            obj = super.clone();
            return (WeeklyLog) obj;
        } catch (CloneNotSupportedException e) {
            System.out.println("不支持复制！");
            return null;
        }
    }

    /**
     * 使用序列化技术实现深克隆
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws OptionalDataException
     */
    public WeeklyLog deepClone() throws IOException, ClassNotFoundException, OptionalDataException {
        //将对象写入流中
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);
        //将对象从流中取出
        ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (WeeklyLog) ois.readObject();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }
}
